package com.borrowed.book.user.controller;

import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.TableDataInfo;
import com.borrowed.book.user.domain.SUser;
import com.borrowed.book.user.domain.SelectUserDto;
import com.borrowed.book.user.domain.SelectUserInDto;
import com.borrowed.book.user.service.SUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户管理")
@RequestMapping("/api/user")
public class UserController extends BaseController {
    @Autowired
    private SUserService userService;

    @ApiOperation(value = "用户一览",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public TableDataInfo selectMemberCard(SelectUserInDto param) {
        startPage();
        try {
            List<SelectUserDto> data=userService.selectUserList(param);
            return getDataTable(data);
        }catch (Exception e){
            e.printStackTrace();
            return getErrorInfo(e.getMessage());
        }
    }

    @ApiOperation(value = "用户信息更新",httpMethod = "PUT")
    @PutMapping("/update")
    @ResponseBody
    public AjaxResult updateUserInfo(@RequestBody  SUser user){
        AjaxResult result;
        try{
            userService.updateUser(user);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error();
        }
        return result;
    }
}
